package com.lc.soft.service.impl;

import com.lc.soft.dao.IOrganizationDao;
import com.lc.soft.entity.CommonParameter;
import com.lc.soft.entity.OrganizationEntity;
import com.lc.soft.service.IOrganizationService;
import com.lc.soft.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class OrganizationService implements IOrganizationService {

    private IOrganizationDao organizationDao;

    @Autowired
    public OrganizationService(IOrganizationDao organizationDao) {
        this.organizationDao = organizationDao;
    }

    @Override
    public ResponseResult create(OrganizationEntity organizationEntity) {
        try {
            int createResult = organizationDao.create(organizationEntity);
            if (createResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(OrganizationEntity organizationEntity) {
        try {
            int updateResult = organizationDao.update(organizationEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult updateState(OrganizationEntity organizationEntity) {
        try {
            int updateResult = organizationDao.updateState(organizationEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findOrganizationListByCondition(CommonParameter commonParameter) {
        try {
            List<OrganizationEntity> organizationEntityList = organizationDao.findOrganizationListByCondition(commonParameter);
            if (organizationEntityList == null || organizationEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            return ResponseResult.success(organizationEntityList);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findPagedOrganizationListByCondition(CommonParameter commonParameter) {
        try {
            List<OrganizationEntity> organizationEntityList = organizationDao.findPagedOrganizationListByCondition(commonParameter);
            if (organizationEntityList == null || organizationEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            int amount = organizationDao.findAmountByCondition(commonParameter);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", organizationEntityList);
            resultMap.put("amount", amount);
            return ResponseResult.success(resultMap);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
